package cj.web.kly.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cj.web.admin.dao.ConfigInfoDAO;
import cj.web.admin.domain.ConfigInfo;
import cj.web.kly.dao.ColumnsDAO;
import cj.web.kly.dao.FriendlyLinkDAO;
import cj.web.kly.dao.GroupInforDAO;
import cj.web.kly.dao.SpecialistLabelDAO;
import cj.web.kly.domain.Columns;
import cj.web.kly.domain.FriendlyLink;
import cj.web.kly.domain.GroupInfor;
import cj.web.kly.domain.SpecialistLabel;
/** 
 * @author ROC 
 * 2010年4月5日18:26:55 
 * 实现getAllInfo方法，取得所有常用字典表里的数据 
 *  
 * */  
public class GetDictionaryInfo {  
	
	/**
	 * https://blog.csdn.net/peng_wu01/article/details/5453361
	 * 
	 * reloadDictionaryInfoMap();实现起来时比较困难的，要在GetDictionaryInfo 取到ServletContext，
	 * 而且要必须取到第一次实例的GetDictionaryInfo ，所有有一个东西很好用，就是Spring的context,取到他的方式
	 * 
	 * 刷新
	 * GetDictionaryInfo.getInstance().reloadDictionaryInfoMap();  
	 * 
	 * 赋值
	 * private static WebApplicationContext springContext;  
	 * springContext = WebApplicationContextUtils.getWebApplicationContext(_servletContext);  
	 */
	public final static String COLUMNS = "COLUMNS_CODE";
	
	public final static String GROUPINFOR = "GROUPINFOR_CODE";
	
	public final static String LABEL = "LABEL_CODE";
	
	public final static String GNJG = "GNJG_CODE";
	
	public final static String GWJG = "GWJG_CODE";
	
	public final static String NSJG = "NSJG_CODE";

	@Autowired
    private ColumnsDAO columnsDAO;
	@Autowired
    private SpecialistLabelDAO specialistLabelDAO;
	@Autowired
    private GroupInforDAO groupInforDAO;
	@Autowired
    private ConfigInfoDAO configInfoDAO;
	@Autowired
    private FriendlyLinkDAO friendlyLinkDAO;
      
    public GetDictionaryInfo(){  
        super();  
    }  
    private static WebApplicationContext springContext;//这个是为了取springContext  
    public static ServletContext _servletContext;//这个是为了取servletContext  
    private static GetDictionaryInfo _instance;   
    public static HashMap<String,Object> dictionaryInfoMap = new HashMap<String, Object>();  
    
    public static GetDictionaryInfo getInstance() {  
        springContext = WebApplicationContextUtils  
        .getWebApplicationContext(_servletContext);  
        if(null == _instance)  
            _instance = (GetDictionaryInfo)springContext.getBean("getDictionaryInfo");  
        return _instance;  
          
    }  
    
    public  void getAllInfo() throws NumberFormatException, Exception {  
        System.out.println("%%%%%%%%%%系统启动加载......");  
        String columnsCodeValue = "";
        String groupInforCodeValue = "";
        String labelValue = "";
        String gnjgValue = "";
    	String gwjgValue = "";
    	String nsjgValue = "";
        
    	//栏目新闻
		List<ConfigInfo> configInfolis = configInfoDAO.queryByCode(COLUMNS);
		if(configInfolis!=null && configInfolis.size()>0){
			columnsCodeValue = configInfolis.get(0).getValue();
		}
		
		//部门组织
		configInfolis = configInfoDAO.queryByCode(GROUPINFOR);
		if(configInfolis!=null && configInfolis.size()>0){
			groupInforCodeValue = configInfolis.get(0).getValue();
		}
		
		//学术
		configInfolis = configInfoDAO.queryByCode(LABEL);
		if(configInfolis!=null && configInfolis.size()>0){
			labelValue = configInfolis.get(0).getValue();
		}
		
		//国内机构
		configInfolis = configInfoDAO.queryByCode(GNJG);
		if(configInfolis!=null && configInfolis.size()>0){
			gnjgValue = configInfolis.get(0).getValue();
		}
		
		//国外机构
		configInfolis = configInfoDAO.queryByCode(GWJG);
		if(configInfolis!=null && configInfolis.size()>0){
			gwjgValue = configInfolis.get(0).getValue();
		}
		
		//内设机构
		configInfolis = configInfoDAO.queryByCode(NSJG);
		if(configInfolis!=null && configInfolis.size()>0){
			nsjgValue = configInfolis.get(0).getValue();
		}
		
		List<Map> configlis = configInfoDAO.queryAll();
		Map configRep = new HashMap();
		for(Map rep:configlis){
			configRep.put(rep.get("code"), rep.get("value"));
		}
		
		List<Map> repColumnslis = getColumnsRel(Integer.parseInt(columnsCodeValue));
		
		List<Map> repGroupInforLis = getGroupInforRel(Integer.parseInt(groupInforCodeValue));
		
		List<Map> repLabelLis = getLabelRel(Integer.parseInt(labelValue));
		
		List<Map> repGnjgLis = getFriendlyLinkRel(Integer.parseInt(gnjgValue));
		
		List<Map> repGwjgLis = getFriendlyLinkRel(Integer.parseInt(gwjgValue));
		
		List<Map> repNsjgLis = getFriendlyLinkRel(Integer.parseInt(nsjgValue));
		
		dictionaryInfoMap.put("configRep", configRep);
		dictionaryInfoMap.put("repColumnslis", JSONArray.fromObject(repColumnslis));
		dictionaryInfoMap.put("repGroupInforLis", JSONArray.fromObject(repGroupInforLis));
		dictionaryInfoMap.put("repLabelLis", JSONArray.fromObject(repLabelLis));
		dictionaryInfoMap.put("repGnjgLis", JSONArray.fromObject(repGnjgLis));
		dictionaryInfoMap.put("repGwjgLis", JSONArray.fromObject(repGwjgLis));
		dictionaryInfoMap.put("repNsjgLis", JSONArray.fromObject(repNsjgLis));
		
        System.out.println("@@@@@@@@@@数据打包完毕，全部放进了dictionaryInfoMap！");  
    }  
    
    /**
	 * 获取栏目lis
	 * @param parentId			父Id
	 * @return
	 * @throws Exception
	 */
	public List<Map> getGroupInforRel(Integer parentId) throws Exception {  
        List<Map> opeCateInfoList = groupInforDAO.queryAllForMap();
        List<Map> retList = groupInforTree(parentId,opeCateInfoList); 
        return retList;  
    }  
    
    /**
     * 递归构造树结构数据
     * @param parentId			父Id
     * @param opeCateInfoList	操作lis
     * @return
     * @throws Exception
     */
    private List<Map> groupInforTree(Integer parentId, List<Map> opeCateInfoList) throws Exception {  
          
        List<Map> interRes = opeCateInfoList;  
        List<Map> retList = new ArrayList<Map>();  
        //根据父节点ID 获取所有 关系  
        List<GroupInfor> rsList = groupInforDAO.queryByPid(parentId);
        if(rsList != null && rsList.size() > 0){  
            for(GroupInfor item : rsList){  
                Map data = new HashMap<String,Object>();  
                for(Map map : interRes){  
                    if(((Integer)map.get("id")).intValue() == item.getId()){  
                        data.put("name", (String)map.get("name"));  
                    }  
                } 
                data.put("id", item.getId()); 
                data.put("key", item.getValue()); 
                data.put("pId", item.getpId());  
                data.put("value", item.getEn());  
                //以当前idOpeCate为父节点，递归查询 所有 关系  
                List<Map> childList = groupInforTree(item.getId(),interRes);  
                if(childList != null && childList.size() > 0){  
                    data.put("item", childList);  
                }  
                retList.add(data);  
            }  
        }  
          
        return retList;  
    }
    
    /**
	 * 获取栏目lis
	 * @param parentId			父Id
	 * @return
	 * @throws Exception
	 */
	public List<Map> getLabelRel(Integer parentId) throws Exception {  
        List<Map> opeCateInfoList = specialistLabelDAO.queryAllForMap();
        List<Map> retList = labelTree(parentId,opeCateInfoList); 
        return retList;  
    }  
    
    /**
     * 递归构造树结构数据
     * @param parentId			父Id
     * @param opeCateInfoList	操作lis
     * @return
     * @throws Exception
     */
    private List<Map> labelTree(Integer parentId, List<Map> opeCateInfoList) throws Exception {  
          
        List<Map> interRes = opeCateInfoList;  
        List<Map> retList = new ArrayList<Map>();  
        //根据父节点ID 获取所有 关系  
        List<SpecialistLabel> rsList = specialistLabelDAO.queryByPid(parentId);
        if(rsList != null && rsList.size() > 0){  
            for(SpecialistLabel item : rsList){  
                Map data = new HashMap<String,Object>();  
                for(Map map : interRes){  
                    if(((Integer)map.get("id")).intValue() == item.getId()){  
                        data.put("name", (String)map.get("name"));  
                    }  
                } 
                data.put("id", item.getId()); 
                data.put("key", item.getValue()); 
                data.put("pId", item.getpId());  
                data.put("value", item.getEn());  
                //以当前idOpeCate为父节点，递归查询 所有 关系  
                List<Map> childList = labelTree(item.getId(),interRes);  
                if(childList != null && childList.size() > 0){  
                    data.put("item", childList);  
                }  
                retList.add(data);  
            }  
        }  
          
        return retList;  
    }
	
	/**
	 * 获取栏目lis
	 * @param parentId			父Id
	 * @return
	 * @throws Exception
	 */
	public List<Map> getColumnsRel(Integer parentId) throws Exception {  
        List<Map> opeCateInfoList = columnsDAO.queryAllForMap();
        List<Map> retList = columnsTree(parentId,opeCateInfoList); 
        return retList;  
    }  
  
  
    /**
     * 递归构造树结构数据
     * @param parentId			父Id
     * @param opeCateInfoList	操作lis
     * @return
     * @throws Exception
     */
    private List<Map> columnsTree(Integer parentId, List<Map> opeCateInfoList) throws Exception {  
          
        List<Map> interRes = opeCateInfoList;  
        List<Map> retList = new ArrayList<Map>();  
        //根据父节点ID 获取所有 关系  
        List<Columns> rsList = columnsDAO.queryByPid(parentId);
        if(rsList != null && rsList.size() > 0){  
            for(Columns item : rsList){  
                Map data = new HashMap<String,Object>();  
                for(Map map : interRes){  
                    if(((Integer)map.get("id")).intValue() == item.getId()){  
                        data.put("name", (String)map.get("name"));  
                    }  
                }  
                data.put("id", item.getId()); 
                data.put("key", item.getValue()); 
                data.put("pId", item.getpId());  
                data.put("value", item.getEn());  
                data.put("isLogin", item.getIsLogin()); 
                
                //以当前idOpeCate为父节点，递归查询 所有 关系  
                List<Map> childList = columnsTree(item.getId(),interRes);  
                if(childList != null && childList.size() > 0){  
                    data.put("item", childList);  
                }else{
                	data.put("item", "");
                }
                retList.add(data);  
            }  
        }  
          
        return retList;  
    }
    
    
    /**
	 * 获取栏目lis
	 * @param parentId			父Id
	 * @return
	 * @throws Exception
	 */
	public List<Map> getFriendlyLinkRel(Integer parentId) throws Exception {  
        List<Map> opeCateInfoList = friendlyLinkDAO.queryAllForMap();
        List<Map> retList = friendlyLinkTree(parentId,opeCateInfoList); 
        return retList;  
    }  
    
    /**
     * 递归构造树结构数据
     * @param parentId			父Id
     * @param opeCateInfoList	操作lis
     * @return
     * @throws Exception
     */
    private List<Map> friendlyLinkTree(Integer parentId, List<Map> opeCateInfoList) throws Exception {  
          
        List<Map> interRes = opeCateInfoList;  
        List<Map> retList = new ArrayList<Map>();  
        //根据父节点ID 获取所有 关系  
        List<FriendlyLink> rsList = friendlyLinkDAO.queryByPid(parentId);
        if(rsList != null && rsList.size() > 0){  
            for(FriendlyLink item : rsList){  
                Map data = new HashMap<String,Object>();  
                for(Map map : interRes){  
                    if(((Integer)map.get("id")).intValue() == item.getId()){  
                        data.put("name", (String)map.get("name"));  
                    }  
                } 
                data.put("id", item.getId()); 
                data.put("key", item.getValue()); 
                data.put("pId", item.getpId());  
                data.put("value", item.getEn());  
                //以当前idOpeCate为父节点，递归查询 所有 关系  
                List<Map> childList = friendlyLinkTree(item.getId(),interRes);  
                if(childList != null && childList.size() > 0){  
                    data.put("item", childList);  
                }  
                retList.add(data);  
            }  
        }  
          
        return retList;  
    }
      
    /** 
     * 重新加载字典表数据！ 
     * @throws Exception 
     * @throws NumberFormatException 
     * */  
    public void reloadDictionaryInfoMap() throws NumberFormatException, Exception {  
        System.out.println("==========~~~~~~~~~~系统开始重新加载字典表");  
        dictionaryInfoMap.clear();  
        System.out.println("==========~~~~~~~~~~字典表MAP,清空成功！");  
        getAllInfo();  
        System.out.println("==========~~~~~~~~~~重新加载字典表成功");  
    }  
      
    public HashMap<String, Object> getDictionaryInfoMap() {  
        return dictionaryInfoMap;  
    }  
    public void setDictionaryInfoMap(HashMap<String, Object> dictionaryInfoMap) {  
        this.dictionaryInfoMap = dictionaryInfoMap;  
    }  
}  